
$(".tabBarBox").load("../../common-html/resource-topTab.html");
var dataall=""
//查询
function searchList() {

    $.ajax({
        url:root.sroreinter.findFloor,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        contentType: "application/json",
        data: JSON.stringify({
            "pageNum":1,
            "pageSize":20,
            "status":$("#status").val(),
            "floorName":$("#floorName").val(),
        }),
        success: function (data) {
            dataall=data.data
            couponlist(data.data, 0);
            $('.M-box3').pagination({
                pageCount: data.data.pageCount,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api) {
                    $.ajax({
                        url: root.sroreinter.findFloor,
                        cache: false,  //禁用缓存
                        type: "POST",
                        dataType:'json',
                        contentType: "application/json",
                        data: JSON.stringify({
                            "pageNum":api.getCurrent(),
                            "pageSize":20,
                            "status":$("#status").val(),
                            "floorName":$("#floorName").val(),

                        }),
                        success: function (result) {
                            couponlist(result.data);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest);
                        }
                    });
                }
            });

        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}
var dataSource = new Array();
var table;   //table对象
dataSource = [];
function couponlist(data, index){
    sessionStorage.setItem("authorizedUsers", JSON.stringify(data));
    dataSource.splice(0,dataSource.length);//清空数组
    if(data !=null && data != ''){
        var datashu =data.startRow+1;
        $.each(data.data, function (i, field) {
            var rowData = new Array();
            rowData.push(field.floorName);
            rowData.push(field.floorOrder);
            rowData.push(field.updateUser);
            rowData.push(field.createTime);
            rowData.push(field.status);
            rowData.push(field.id);
            dataSource.push(rowData);

        });
    }
    initDataTable(dataSource);
    function initDataTable(dataSource) {

        var dttable = $('#datatable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            "autoWidth": false,
            "ordering": true,
            "searching": true,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": [50, 25, 10, 5],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[0]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 0
                },

                {
                    "render": function (data, type, row, meta) {
                        var val = row[1]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 1
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[2]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 2
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[3]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets":3
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[4]
                        if (val == 0) {
                            val = '生效'
                        }else if (val == 1) {
                            val = '失效'
                        }
                        return val;
                    },
                    "targets": 4
                },
                {
                    "render": function (data, type, row, meta) {
                      var val='<a  onclick="floorManagementadd(2,\''+row[0]+'\',\''+row[1]+'\',\''+row[4]+'\',\''+row[5]+'\')">编辑</a><a class="ml-10" onclick="jumpData(\'编辑\',\'floormanagementEditor.html\',\''+row[5]+'\',\''+row[0]+'\')">楼层管理</a><a class="ml-10" onclick="floorManagementdeleta(\''+row[5]+'\')">删除</a>'
                        return val;
                    },
                    "targets":5
                }


            ]
        });
        //权限控制函数
        power()
    };
}





//删除
function floorManagementdeleta(id) {
        var curInd = layer.confirm(
            '<span>确定要删除吗？</span>'+'<br/>', {offset:'230px',title:'提示',btn: ['确定', '取消']}, function(index){

                $.ajax({
                    url:root.sroreinter.updateFloor,
                    cache: false,  //禁用缓存
                    type: "POST",
                    dataType:'json',
                    contentType: "application/json",
                    data: JSON.stringify({
                        "id":id,
                        "status":2,
                    }),
                    success: function (data) {
                        if(data.errorCode==0){
                                $.Huimodalalert('删除成功',2000);
                            searchList()
                        }else {
                            $.Huimodalalert(data.msg,2000);

                        }

                    },
                    error: function () {
                        $.Huimodalalert('失败！',2000);
                    }
                });

                layer.close(curInd);

            },function(){
                layer.close(curInd);

            });

}



//新增
var pages=""
function floorManagementadd(val,name,order,status,id) {
    $("#floorManagementadd").modal("show")
    if(val==2){
        pages="编辑"
        $("#floor_name").val(name)
        $("#floor_order").val(order)
        $("#floor_status").val(status)
        $("#floorid").val(id)
        $("#floor_status").removeAttr("disabled")
    }else if(val==1) {
        pages="新增"
        $("#floor_name").val("")
        $("#floor_order").val("")
        $("#floor_status").val(1)
        $("#floorid").val("")
        $("#floor_status").attr("disabled","disabled")
    }

}
//保存
function preservationdata() {
    if($("#floor_name").val()==""){
        $.Huimodalalert('请填写楼层名称',2000);
        return false;
    }
    if($("#floor_order").val()==""){
        $.Huimodalalert('请填写展示顺序',2000);
        return false;
    }
    var urls=""
    if(pages=="编辑"){
        urls=root.sroreinter.updateFloor
    }else {
        urls=root.sroreinter.addFloor
    }
    $.ajax({
        url:urls,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        contentType: "application/json",
        data: JSON.stringify({
            "id":$("#floorid").val(),
            "floorName":$("#floor_name").val(),
            "floorOrder":$("#floor_order").val(),
            "status":$("#floor_status").val(),
            "floorUrl":"",
            "updateUser":JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name
        }),
        success: function (data) {
            if(data.errorCode==0){
                if(pages=="编辑"){
                    $.Huimodalalert('编辑成功',2000);
                }else {
                    $.Huimodalalert('添加成功',2000);
                }
                $("#floorManagementadd").modal("hide")
                searchList()

            }else {
                $.Huimodalalert(data.msg,2000);

            }

        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });

}
//取消
function closedata() {
    $("#floorManagementadd").modal("hide")
}
//跳转
function jumpData(title,url,id,name) {
    sessionStorage.setItem("floorManagementid",id)
    sessionStorage.setItem("floorManagementname",name)
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}
/*
下拉框的展示
 */
$(function () {
    $.ajax({
        type: "post",
        url: root.sroreinter.get_category,
        cache: false,  //禁用缓存
        dataType: "json",
        data: {
            category_id:1,type:101
        },  //传入组装的参数
        success: function (data) {
            if(data.errorCode=="0") {
                var dataInter=data.data;
                var interText = doT.template($("#ascription_bob").text());
                $("#category_id").html(interText(dataInter));
                console.log(dataInter)
                $.each(dataInter,function(inx,data2){
                    if(data2.name=="楼层配置"){
                        var member='<option selected value="'+data2.category_id+'" >'+data2.name+'</option>';
                        $('#category_id').append(member)
                    }else {
                        var member='<option value="'+data2.category_id+'" >'+data2.name+'</option>';
                        $('#category_id').append(member)
                    }
                })

            }
        }
    })
})
function discount() {
    $("#category_id").change(function(event) {
        var fanx = $(this).find("option:selected");
        //通栏配置 3 4 5 10
        if(fanx.val()==3){
            window.location.href="../resource-html/resource.html?value="+"拉幕配置"
        }
        if(fanx.val()==4){
            window.location.href="../resource-html/resource.html?value="+"浮框配置"
        }
        if(fanx.val()==5){
            window.location.href="../resource-html/resource.html?value="+"弹框配置"
        }
        if(fanx.val()==2){
            window.location.href="../top-html/top.html"
        }
        //轮播图配置
        if(fanx.val()==6){
            window.location.href="../picture-html/picture.html"
        }
        //搜索栏配置
        if(fanx.val()==7){
            window.location.href="../search-html/search.html"
        }
        //品类展示区配置
        if(fanx.val()==18){
            window.location.href="../display-html/display.html"
        }
        if(fanx.val()==21){
            window.location.href="../categorylogo-html/categorylogo.html"
        }
        if(fanx.val()==22){
            window.location.href="../brandhalllogo-html/brandhalllogo.html"
        }
    });
}